package com.jcli._定时提醒;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 在界面文本添加显示剩余时间
 * 右下角也能提示了。
 */
public class FocusAssistantApp04 {
    private static final int ONE_SECOND = 1000; // 1秒的毫秒数
    // private static final int FOCUS_TIME = 20 * ONE_MINUTE; // 20分钟的毫秒数
    private static final int FOCUS_TIME = 7000; // 20分钟的毫秒数

    private static Timer timer;
    private static long remainingTime = FOCUS_TIME;
    private static boolean trayIconCreated = false;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("专注助手");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JButton startButton = new JButton("开始专注计时器");
        JButton stopButton = new JButton("停止专注计时器");

        JLabel timeLabel = new JLabel("剩余时间: 20 分 0 秒");

        startButton.addActionListener(e -> startFocusTimer(timeLabel));
        stopButton.addActionListener(e -> stopFocusTimer());

        panel.add(startButton);
        panel.add(stopButton);
        panel.add(timeLabel);

        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // 在程序启动时就创建托盘图标
        createTrayIcon();
    }

    private static void startFocusTimer(JLabel timeLabel) {
        stopFocusTimer(); // 如果有正在运行的计时器，先停止

        timer = new Timer(ONE_SECOND, e -> updateRemainingTime(timeLabel));
        timer.setRepeats(true); // 设置为true，定时器一直运行
        timer.start();
    }

    private static void stopFocusTimer() {
        if (timer != null && timer.isRunning()) {
            timer.stop();
        }
    }

    private static void createTrayIcon() {
        if (!trayIconCreated && SystemTray.isSupported()) {
            try {
                SystemTray tray = SystemTray.getSystemTray();

                // 加载托盘图标（根据实际路径调整）
                Image image = Toolkit.getDefaultToolkit().getImage("pic/akx.png");

                // 创建弹出菜单
                PopupMenu popupMenu = new PopupMenu();
                MenuItem exitItem = new MenuItem("退出");
                exitItem.addActionListener(e -> System.exit(0));
                popupMenu.add(exitItem);

                // 创建托盘图标
                TrayIcon trayIcon = new TrayIcon(image, "专注助手", popupMenu);
                trayIcon.setImageAutoSize(true);

                // 将托盘图标添加到系统托盘
                tray.add(trayIcon);

                trayIconCreated = true;
            } catch (AWTException ex) {
                ex.printStackTrace();
            }
        }
    }

    private static void updateRemainingTime(JLabel timeLabel) {
        remainingTime -= ONE_SECOND;
        if (remainingTime <= 0) {
            stopFocusTimer();
            remainingTime = 0;
            showNotification("休息一下！", "您已完成专注时间。");
        }

        // 更新界面上的剩余时间显示
        updateUI(timeLabel);

        // 更新托盘图标的提示文本
        updateTrayIconToolTip(timeLabel.getText());
    }

    private static void updateUI(JLabel timeLabel) {
        long minutes = remainingTime / ONE_SECOND / 60;
        long seconds = (remainingTime / ONE_SECOND) % 60;
        timeLabel.setText("剩余时间: " + minutes + " 分 " + seconds + " 秒");
    }

    private static void updateTrayIconToolTip(String toolTip) {
        // 获取系统托盘
        SystemTray tray = SystemTray.getSystemTray();

        // 获取托盘图标
        TrayIcon trayIcon = tray.getTrayIcons()[0];

        // 在托盘图标上显示计时器剩余时间
        trayIcon.setToolTip(toolTip);
    }

    private static void showNotification(String title, String message) {
        // 在通知中显示消息
        JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
    }
}
